<script lang="ts" setup>
import mainEmit from "@/utils/bus";
import { ref, reactive } from "vue";
import { FormInstance } from "element-plus";
import { verifyMobile } from "@/utils/verify";
import { LoginApi, CommonApi } from "@/Api";

const formRef = ref<FormInstance>();
const sendLoading = ref(false);
const sendDisabled = ref(false);
const sendText = ref("发送验证码");
const loading = reactive({
   submit: false,
   send: false
});
const disable = reactive({
   submit: false,
   send: false
});

const formData = reactive({
   account: "",
   smgId: "",
   code: ""
});

/** 发送短信 */
const sendSmg = async () => {
   if (!verifyMobile.test(formData.account)) {
      ElMessage.error("手机号格式错误");
      return;
   }
   sendLoading.value = true;
   sendDisabled.value = true;

   const { data, code } = await CommonApi.SendMessage({ phone: formData.account });
   if (code == 200) formData.smgId = data.smgId;
   sendLoading.value = false;

   let time = 60;
   const timer = setInterval(() => {
      time--;
      sendText.value = time + "秒";
      if (time == 0) {
         clearInterval(timer);
         sendDisabled.value = false;
         sendText.value = "发送验证码";
      }
   }, 1000);
};

// 登录
function submit(formRef?: FormInstance) {
   if (!formRef) return;
   loading.submit = true;
   disable.submit = true;

   formRef.validate(async (valid, fields) => {
      if (!valid) return;

      const { data, code } = await LoginApi.Message(formData);
      if (code === 200) {
         mainEmit.emit("loginResult", data);
      }
   });

   loading.submit = false;
   disable.submit = false;
}
</script>

<template>
   <el-form ref="formRef" class="elForm" :model="formData" label-width="auto">
      <el-form-item>
         <template v-slot:label>
            <span class="label">账号</span>
         </template>
         <el-input v-model="formData.account" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item>
         <template v-slot:label>
            <span class="label">验证码</span>
         </template>
         <el-input class="send" v-model="formData.code" @keyup.enter="submit" placeholder="请输入验证码">
            <template #append>
               <el-tooltip :content="sendText" placement="bottom" effect="light">
                  <el-button @click="sendSmg" :loading="sendLoading" :disabled="sendDisabled">
                     <el-icon v-if="!sendLoading"><Promotion /></el-icon>
                  </el-button>
               </el-tooltip>
            </template>
         </el-input>
      </el-form-item>
      <el-form-item class="elButton">
         <el-button @click="submit(formRef)" :loading="loading.submit" :disabled="disable.submit">登录</el-button>
      </el-form-item>
   </el-form>
</template>

<style lang="scss" scoped>
.elForm {
   width: 100%;
   :deep(.el-input) {
      background-color: transparent;
      .el-input__wrapper {
         background-color: rgba($color: #fff, $alpha: 0.1);
         transition: all 0.3s;
         // border: 1px solid rgba($color: #fff, $alpha: 0.5);
         box-shadow: 0 0 0 1px rgba($color: #fff, $alpha: 0.3) inset;

         .el-input__inner {
            background-color: transparent;
            color: #000;
         }
      }
      .is-focus {
         background-color: rgba($color: #fff, $alpha: 0.5);
      }
   }

   :deep(.send) {
      .el-input-group__append {
         width: 20px;
         background-color: rgba($color: #fff, $alpha: 0.1);
         border: 1px solid rgba($color: #fff, $alpha: 0.3);
         border-left: none;
         color: #fff;
         transition: all 0.3s;
         box-shadow: none;

         &:hover {
            background-color: rgba($color: #fff, $alpha: 0.3);
            border: 1px solid rgba($color: #fff, $alpha: 0.5);
            border-left: none;
         }
         .is-loading {
            > span {
               display: none;
            }
            &::before {
               background-color: transparent;
            }
         }
      }
   }
}
.label {
   color: #fff;
}

.elButton {
   :deep(.el-button) {
      margin-top: 30px;
      width: 100%;
      background-color: transparent;
      border: 1px solid rgba($color: #fff, $alpha: 0.3);
      color: #fff;
      transition: all 0.3s;

      &:hover {
         background-color: rgba($color: #fff, $alpha: 0.5);
         color: #000;
      }
   }
}
</style>
